import pinia from "@/store/store";
import { channelOptions, drawerFormState } from "./store";
import { tableLoading } from "./table";
const dictsStore = useDictsStore(pinia);
const fetchDicts = (dicts = ref<any>({}), params: any = {}) =>
  new Promise(async (resolve) => {
    const res = await userApi.queryDicts(params);
    dictsStore.setState(res.data ?? {});
    resolve(storeToRefs(dictsStore).getState.value);
  });
export const fetchSearchMap = async () => {
  const res: any = await fetchDicts();
  channelOptions.value = arrayToOptions(res.channel, {
    label: "name",
    value: "id",
  });
};

export const fetchDataById = async (params = {}) => {
  try {
    tableLoading.value = true;
    const res: any = await payApi.selectDataById(params);
    const { data = {} } = res;
    drawerFormState.order_number = data.order_number;
    drawerFormState.third_order_number = data.third_order_number;
    drawerFormState.status = data.status;
    drawerFormState.order_status = data.order_status;
    drawerFormState.channel_name = data.channel_name;
    drawerFormState.phone_number = data.phone_number;
    drawerFormState.pay_type = data.pay_type;
    drawerFormState.user_info = data.user_info;
    drawerFormState.create_time = data.create_time;
    drawerFormState.origin_type = data.origin_type;
    drawerFormState.payment_time = data.payment_time;
    drawerFormState.dataSource[0] = data?.member_info ?? {};
    return data;
  } catch (error: any) {
    message.error(error.message);
  } finally {
    tableLoading.value = false;
  }
};
